﻿@model OT.Areas.Admin.Models.Tests

<div class="modal-content">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="fa fa-times"></i></button>
        <h3 id="myModalLabel">Test Edit</h3>
    </div>
    <div>
        @Html.HiddenFor(model => model.TestID, new { id = "HDFID" })
        @Html.Hidden("QuetionIDs", null, new { id = "QuetionIDs" })
        @Html.Hidden("StudentIDs", null, new { id = "StudentIDs" })
        <div class="modal-body">
            <div id="alert_modal"></div>
            <div class="form-vertical" role="form">
                <div class="form-group">
                    <div class="col-lg-13">
                        <div id="divMsgEdit"></div>
                    </div>
                </div>
            </div>
            <div class="form-vertical" role="form">

                <div class="form-group" id="divCategoryName">
                    <label class="col-lg-2 control-label" style="text-align:right">Category Name</label>
                    <div class="col-lg-4">
                        @Html.DropDownListFor(model => model.CategoryID, new SelectList(Model.cat, "CategoryID", "CategoryName"), "--- Select ---", new { @class = "form-control" })
                        @Html.ValidationMessageFor(model => model.CategoryID)
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-lg-2 control-label" style="text-align:right">Available Question</label>
                    <div class="col-lg-4">
                        <div id="TotAvail" style="color: green; font-size: 16px;">@Model.totalAvailQue</div>
                    </div>
                </div>
            </div>
            <div class="clearfix"></div>
            <div class="form-vertical" role="form">
                <div class="form-group" id="divSubject">
                    <label class="col-lg-2 control-label" style="text-align:right">Subject</label>
                    <div class="col-lg-4">
                        @Html.DropDownListFor(model => model.SubjectID, new SelectList(Model.Subjects, "SubjectID", "SubjectName"), "--- Select ---", new { @class = "form-control" })
                        @Html.ValidationMessageFor(model => model.SubjectID)
                    </div>
                </div>
                <div class="form-group" id="divTotalQuestion">
                    <label class="col-lg-2 control-label" style="text-align:right">Total Question</label>
                    <div class="col-lg-2">
                        @Html.TextBoxFor(model => model.TotalQuestion, new { @class = "form-control", @placeholder = "No of Question", @maxlength = "2" })
                    </div>
                    <div class="col-lg-2">
                        <a href="/Instructor/Test/ShowAllQuestion?subjectId=" id="Custom">Custom Question</a>
                    </div>
                </div>
            </div>
            <div class="clearfix"></div>
            <div class="form-vertical" role="form">
                <div class="form-group" id="divTopic">
                    <label class="col-lg-2 control-label" style="text-align:right">Topic</label>
                    <div class="col-lg-4">
                        @Html.DropDownListFor(model => model.TopicID, new SelectList(Model.Topics, "TopicID", "TopicName"), "--- Select ---", new { @class = "form-control" })
                        @Html.ValidationMessageFor(model => model.TopicID)
                    </div>
                </div>
                <div class="form-group" id="divPassingScore">
                    <label class="col-lg-2 control-label" style="text-align:right">Passing Score</label>
                    <div class="col-lg-4">
                        @Html.TextBoxFor(model => model.PassingScore, new { @class = "form-control", @placeholder = "In %", @maxlength = "3" })
                        @Html.ValidationMessageFor(model => model.PassingScore)
                    </div>
                </div>
            </div>
            <div class="clearfix"></div>
            <div class="form-vertical" role="form">
                <div class="form-group" id="divTestType">
                    <label class="col-lg-2 control-label" style="text-align:right">Test Type</label>
                    <div class="col-lg-4">
                        @Html.DropDownListFor(model => model.TestTypeID, new SelectList(Model.TestTypes, "TestTypeID", "TestType1"), "-- Select --", new { @class = "form-control" })
                        @Html.ValidationMessageFor(model => model.TestTypeID)
                    </div>
                </div>
                <div class="form-group" id="divDuration">
                    <label class="col-lg-2 control-label" style="text-align:right">Duration Of Test</label>
                    <div class="col-lg-4">
                        @Html.TextBoxFor(model => model.Duration, new { @class = "form-control", @placeholder = "[in Minute]", @maxlength = "3" })
                        @Html.ValidationMessageFor(model => model.Duration)
                    </div>
                </div>
            </div>
            <div class="clearfix"></div>
            <div class="form-vertical" role="form">
                <div class="form-group" id="divTestName">
                    <label class="col-lg-2 control-label" style="text-align:right">Test Name</label>
                    <div class="col-lg-4">
                        @Html.TextBoxFor(model => model.TestName, new { @maxlength = "30", @placeholder = "Enter Test Name", @class = "form-control" })
                        @Html.ValidationMessageFor(model => model.TestName)
                    </div>
                </div>
                <div class="form-group" id="divPositiveMark">
                    <label class="col-lg-2 control-label" style="text-align:right">Positive Mark</label>
                    <div class="col-lg-4">
                        @Html.TextBoxFor(model => model.PositiveMark, new { @class = "form-control", @placeholder = "Mark", @maxlength = "3" })
                        @Html.ValidationMessageFor(model => model.PositiveMark)
                    </div>
                </div>
            </div>
            <div class="clearfix"></div>
            <div class="form-vertical" role="form">
                <div class="form-group" id="divDescription">
                    <label class="col-lg-2 control-label" style="text-align:right">Description</label>
                    <div class="col-lg-4">
                        <textarea class="form-control" placeholder="Enter Description" id="Description" rows="1" maxlength="50">@Model.Description</textarea>
                    </div>
                </div>
                <div class="form-group" id="divNegativeMark">
                    <label class="col-lg-2 control-label" style="text-align:right">Negative Mark</label>
                    <div class="col-lg-4">
                        @Html.TextBoxFor(model => model.NegativeMark, new { @class = "form-control", @placeholder = "Mark", @maxlength = "3" })
                        @Html.ValidationMessageFor(model => model.NegativeMark)
                    </div>
                </div>
            </div>
 <div class="clearfix"></div>
 <br />
            <div class="clearfix"></div>
             <div class="form-vertical" role="form">
                <div class="form-group" id="d">
                    <label class="col-lg-2 control-label" style="text-align: right">Select Students</label>
                    <div class="col-lg-2">
                    <a href="/Instructor/Test/GetAllStudent" id="Students">Custom Students</a>
                    </div>
                    <label class="label label-info" style="font-size:16px;" id="lblstudentcnt">@Model.totalStudent</label>
                </div>
            </div>
           <div class="clearfix"></div>

        </div>
        <div class="clearfix"></div>
    </div>
    <div class="modal-footer">
        <button class="btn btn-default" type="submit" id="btnEdit"><i class="icon-cog"></i>Save</button>
        <button class="btn btn-default" data-dismiss="modal" aria-hidden="true"><i class="icon-off"></i>Cancel</button>
    </div>
</div>

<script type="text/javascript">
    $(document).ready(function () {
        var te = $('#totalAvailQue').val();
        $('#TotAvail').html(te);
        $("#lblstudentcnt").text();
    });
    $("#btnEdit").click(function (e) {
        var TestID = $("#HDFID").val();
        var TestTypeID = $("#TestTypeID option:selected").val();
        var CategoryID = $("#CategoryID option:selected").val();
        var SubjectID = $("#SubjectID option:selected").val();
        var TopicID = $("#TopicID option:selected").val();
        var TestName = $("#TestName").val();
        var Description = $("#Description").val();
        var TotalQuestion = $("#TotalQuestion").val();
        var QuestionIDs = $("#QuetionIDs").val();
        var StudentIDs = $("#StudentIDs").val();
        var Duration = $("#Duration").val();
        var PassingScore = $("#PassingScore").val();
        var PositiveMark = $("#PositiveMark").val();
        var NegativeMark = $("#NegativeMark").val();
        e.preventDefault();

        if (TestTypeID == 0 || TestName == '' || CategoryID == 0 || SubjectID == 0 || TopicID == 0 || TotalQuestion == '' || Duration == '' || PassingScore == '' || PositiveMark == '' || NegativeMark == '') {
            showMsg('divMsgEdit', 'Please Enter Value in Required Fields', 'danger');
            (TestTypeID == 0) ? $('#divTestType').addClass('has-error') : $('#divTestType').removeClass('has-error');
            (CategoryID == 0) ? $('#divCategoryName').addClass('has-error') : $('#divCategoryName').removeClass('has-error');
            (SubjectID == 0) ? $('#divSubject').addClass('has-error') : $('#divSubject').removeClass('has-error');
            (TopicID == 0) ? $('#divTopic').addClass('has-error') : $('#divTopic').removeClass('has-error');
            (TestName == '') ? $('#divTestName').addClass('has-error') : $('#divTestName').removeClass('has-error');
            (TotalQuestion == '') ? $('#divTotalQuestion').addClass('has-error') : $('#divTotalQuestion').removeClass('has-error');
            (Duration == '') ? $('#divDuration').addClass('has-error') : $('#divDuration').removeClass('has-error');
            (PassingScore == '') ? $('#divPassingScore').addClass('has-error') : $('#divPassingScore').removeClass('has-error');
            (PositiveMark == '') ? $('#divPositiveMark').addClass('has-error') : $('#divPositiveMark').removeClass('has-error');
            (NegativeMark == '') ? $('#divNegativeMark').addClass('has-error') : $('#divNegativeMark').removeClass('has-error');
        }
        else {
            var totavail = Number($("#TotAvail").text());
            var selectque = Number($('#TotalQuestion').val());
            if (selectque > totavail) {
                showMsg('divMsgEdit', 'Total Question is Greater than Available Question', 'danger');
            }
            else {

                $.ajax({
                    url: "/Instructor/Test/Edit?TestID=" + TestID + "&TestName=" + TestName + "&TestTypeID=" + TestTypeID + "&CategoryID=" + CategoryID + "&SubjectID=" + SubjectID + "&TopicID=" + TopicID + "&Description=" + Description + "&TotalQuestion=" + TotalQuestion + "&Duration=" + Duration + "&PassingScore=" + PassingScore + "&PositiveMark=" + PositiveMark + "&NegativeMark=" + NegativeMark + "&QuestionIDs=" + QuestionIDs + "&StudentIDs=" + StudentIDs,
                    type: "POST",
                    success: function (result) {
                        if (result != "NotValid") {
                            $('#modalDiv').modal('hide');
                            showMsg('divMsgIndex', 'Record Updated Successfuly', 'success');
                            loadPage(1, 10, '');
                        }
                        else {
                            showMsg('divMsgEdit', 'Failed To Update Record', 'danger');
                        }
                    },
                    error: function (data) {
                    }
                });
            }
        }
        return false;
    });

    $("#TotalQuestion").keypress(function (e) {
        if ((e.which < 48 || e.which > 57)) {
            if (e.which == 8 || e.which == 46 || e.which == 0) {
                $("#ErrorMsg").css({ "display": "none" });
                return true;
            }
            else {
                showMsg('divMsgEdit', 'Enter only numbers 0-9', 'info');
                return false;
            }
        }
    });

    $("#Duration").keypress(function (e) {
        if ((e.which < 48 || e.which > 57)) {
            if (e.which == 8 || e.which == 46 || e.which == 0) {
                $("#ErrorMsg1").css({ "display": "none" });
                return true;
            }
            else {
                showMsg('divMsgEdit', 'Enter only numbers 0-9', 'info');
                return false;
            }
        }
    });

    $("#PassingScore").keypress(function (e) {
        if ((e.which < 48 || e.which > 57)) {
            if (e.which == 8 || e.which == 46 || e.which == 0) {
                $("#ErrorMsg2").css({ "display": "none" });
                return true;
            }
            else {
             
                showMsg('divMsgEdit', 'Enter only numbers 0-9', 'info');
                return false;
            }
        }
    });

    $("#PositiveMark").keypress(function (e) {
        if ((e.which < 48 || e.which > 57)) {
            if (e.which == 8 || e.which == 46 || e.which == 0) {
                $("#ErrorMsg3").css({ "display": "none" });
                return true;
            }
            else {
                $("#ErrorMsg3").css({ "display": "initial" });
                showMsg('divMsgEdit', 'Enter only numbers 0-9', 'info');
                return false;
            }
        }
    });

    $("#NegativeMark").keypress(function (e) {
        if ((e.which < 48 || e.which > 57)) {
            if (e.which == 8 || e.which == 46 || e.which == 0) {
                $("#ErrorMsg4").css({ "display": "none" });
                return true;
            }
            else {
                showMsg('divMsgEdit', 'Enter only numbers 0-9', 'info');
                return false;
            }
        }
    });
    $("#Custom").click(function (e) {
        var id = $("#SubjectID option:selected").val();
        var tid = $("#TopicID option:selected").val();
        e.preventDefault();
        if (id == 0 || tid == 0) {
            showMsg('divMsgEdit', 'Please Select Subject & Topic', 'danger');
        }
        else {
            var TestID = $("#HDFID").val();
            $("#CustomQuestion").load(this.href + id + "&topicid=" + tid + "&testid=" + TestID, function () {
                $('#modalQsDiv').modal({
                    backdrop: 'static'
                }, 'show');
            });
        }
    });
    
    $("#Students").click(function (e) {
        e.preventDefault();
        $("#CustomStudent").load(this.href, function () {
            $('#modalUserDiv').modal({
                backdrop: 'static'
            }, 'show');
        });
        var TestID = $("#HDFID").val();
        $.ajax({
            url: "/Instructor/Test/GetTestUsers?TestID=" + TestID,
            type: "POST",
            success: function (result) {
                try {
                    var table = document.getElementById("StudentTable");
                    var rowCounts = table.rows.length;
                    var j = 0;
                    for (var i = 0; i < rowCounts; i++) {
                        for (var j = 0; j < result.Data.length; j++) {
                            var chkbox = $(table).find("input#chkTblStudent")[i];
                            var id = Number(chkbox.getAttribute("value"));
                            var uid = Number(result.Data[j].UserID);
                            if (id == uid) {
                                chkbox.checked = true;
                            }
                        }
                    }
                } catch (e) {

                }
            },
            error: function (data) {
            }
        });
    });

    $("#CategoryID").change(function (e) {
        var CategoryID = $(this).val();
        if (CategoryID != 0) {
            $.getJSON("/Student/MyTest/Subjects", { CategoryID: CategoryID },
                   function (classesData) {
                       var select = $("#SubjectID");
                       select.empty();
                       select.append($('<option/>', {
                           value: 0,
                           text: "----Select Subject----"
                       }));
                       $.each(classesData, function (index, itemData) {

                           select.append($('<option/>', {
                               value: itemData.Value,
                               text: itemData.Text
                           }));
                       });
                   });
        }
        else {
            var select = $("#SubjectID");
            select.empty();
            select.append($('<option/>', {
                value: 0,
                text: "----Select Subject----"
            }));
            var select1 = $("#TopicID");
            select1.empty();
            select1.append($('<option/>', {
                value: 0,
                text: "----Select Topic----"
            }));
        }
        return false;
    });

    $("#SubjectID").change(function (e) {
        var SubjectID = $(this).val();
        if (SubjectID != 0) {
            $.getJSON("/Student/MyTest/Topics", { SubjectID: SubjectID },
                   function (classesData) {
                       var select = $("#TopicID");
                       select.empty();
                       select.append($('<option/>', {
                           value: 0,
                           text: "----Select Topic----"
                       }));
                       $.each(classesData, function (index, itemData) {

                           select.append($('<option/>', {
                               value: itemData.Value,
                               text: itemData.Text
                           }));
                       });
                   });
        }
        else {
            var select = $("#TopicID");
            select.empty();
            select.append($('<option/>', {
                value: 0,
                text: "----Select Topic----"
            }));
        }
        return false;
    });

    $("#TopicID").change(function (e) {
        var TopicID = $(this).val();
        var SubjectID = $("#SubjectID option:selected").val();
        if (TopicID == '')
            TopicID = '0';
        $("#totalAvailQue").css({ "display": "none" });
        $.get("/Instructor/Test/GetAvailableQuestion", { SubjectID: SubjectID, TopicID: TopicID }, function (data) {
            $("#TotAvail").html(data);
        });
        return false;
    });


</script>
